#########
#Function for Cluster Robust SEs
#########
fit.w.robust <- function(model, cluster.var, dta){	
	robust.se <- function(model, cluster){
		require(sandwich)
		require(lmtest)
		M <- length(unique(cluster))
		N <- length(cluster)
		K <- model$rank
		dfc <- (M/(M - 1)) * ((N - 1)/(N - K))
		uj <- apply(estfun(model), 2, function(x) tapply(x, cluster, sum));
		rcse.cov <- dfc * sandwich(model, meat = crossprod(uj)/N)
		rcse.se <- coeftest(model, rcse.cov)
		return(list(rcse.cov, rcse.se))
	}
	
	out <- model$model
	clustervar <- mapply(paste,cluster.var,dta[!(1:dim(dta)[1] %in% model$na.action),cluster.var],sep="")
	vcov <- robust.se(model, clustervar)
	model$se <- vcov[[1]]
	model$coeftest <- vcov[[2]]
	return(model)
}

#########
#Load Data and Package to Output Regression Results
#########
load('estimation.data.conjoint.RData')
library('apsrtable')

#############################################
#####Initial Table For Continuous Interactions
#############################################

interact.model.continuous <- lm(chosen.candidate ~ copartisan + information.condition + copartisan*information.condition, data=estimation.data.conjoint)
interact.model.continuous <- fit.w.robust(model=interact.model.continuous,cluster.var='participant.id',dta=estimation.data.conjoint)
predicted.copartisanship.me <- interact.model.continuous$coefficients[2] + interact.model.continuous$coefficients[4]*seq(from=1,to=9,by=.1)

#Conditional on Information Type
interact.model.continuous.w.infotype <-  lm(chosen.candidate ~ copartisan + information.condition + copartisan*information.condition + gender + profession + age + family.status + race + education + abortion.stance + spending.stance, data=estimation.data.conjoint)
interact.model.continuous.w.infotype <- fit.w.robust(model=interact.model.continuous.w.infotype,cluster.var='participant.id',dta=estimation.data.conjoint)

#Conditional on Information Type + Attribute Level
interact.model.continuous.typeandcontent <-  lm(formula=chosen.candidate ~ copartisan + information.condition + copartisan*information.condition + gender.candidate + profession.candidate + age.candidate + family.status.candidate + race.candidate + education.candidate + abortion.stance.candidate + spending.stance.candidate + I(military.service.candidate=="Served in U.S. Military"), data=estimation.data.conjoint)
interact.model.continuous.typeandcontent <- fit.w.robust(model=interact.model.continuous.typeandcontent,cluster.var='participant.id',dta=estimation.data.conjoint)

apsrtable(interact.model.continuous, interact.model.continuous.w.infotype, interact.model.continuous.typeandcontent,  model.names=c("Baseline","Controls: Info Type","Controls: Info Type + Content"), omitcoef=expression(grep(pattern="copartisan|information.condition|Intercept",coefnames, invert=TRUE)), caption="Pr(Prefer Candidate) By Information Amount", coef.names=c("(Intercept)", "Co-Partisan", "Information Amount", "Co-Partisan*Information Amount"), notes=list(se.note="Robust standard errors in parentheses, clustered by respondent.",stars.note="$^*$ indicates significance at $p< 0.05 $"),digits=3)

#############################################
#####Discretized Interactions
#############################################

interact.model.discrete <- lm(chosen.candidate ~ copartisan + factor(information.condition) + copartisan*factor(information.condition), data=estimation.data.conjoint)
interact.model.discrete <- fit.w.robust(model=interact.model.discrete,cluster.var='participant.id',dta=estimation.data.conjoint)

coparty.coefs <- grep(pattern='copartisan',names(interact.model.discrete$coefficients))
discrete.coefs <- c(interact.model.discrete$coefficients[2],sapply(X=coparty.coefs[2:length(coparty.coefs)], FUN=function(x) interact.model.discrete$coefficients[2] + interact.model.discrete$coefficients[x]))
discrete.ses <- c(sqrt(interact.model.discrete$se[2,2]),sapply(X=coparty.coefs[2:length(coparty.coefs)], FUN=function(x) sqrt(interact.model.discrete$se[2,2] + interact.model.discrete$se[x,x] + 2*interact.model.discrete$se[2,x])))
discrete.interact.plot.frame <- cbind.data.frame(discrete.coefs,discrete.ses)
discrete.interact.plot.frame$upper <- discrete.interact.plot.frame[,1] + 2*discrete.interact.plot.frame[,2]
discrete.interact.plot.frame$lower <- discrete.interact.plot.frame[,1] - 2*discrete.interact.plot.frame[,2]

pdf(file='copartisanshipeffectdiscrete.pdf', height=4, width=5.25)
par(mar=c(4,5.5,1.5,4))
plot(y=discrete.interact.plot.frame[,1],x=c(1,3,5,7,9),main='', xlab='Pieces of Candidate Information', las=1, xaxt='n', ylim=c(0,1), ylab='Effect of Co-Partisanship on
Pr(Prefer Candidate)', pch=16, cex=1.5, type='n')
lines(x=seq(from=1,to=9,by=.1), y=predicted.copartisanship.me,col='gray', lwd=3)
points(discrete.interact.plot.frame[,1],x=c(1,3,5,7,9), pch=16, cex=1.25)
axis(side=1, at=c(1,3,5,7,9))
segments(x0=c(1,3,5,7,9), x1=c(1,3,5,7,9), y1=discrete.interact.plot.frame[,3], y0=discrete.interact.plot.frame[,4], lwd=2.5)
dev.off()

#############################################
###Effects of Information Types on Co-partisan Voting - Reference Category is Partisan Voting when Race Info Provided
#############################################

attribute.comparison <- lm(chosen.candidate ~ copartisan + gender + profession + age + family.status + race + military.service + education + abortion.stance + spending.stance + copartisan*gender + copartisan*profession + copartisan*age + copartisan*family.status + copartisan*race + copartisan*military.service + copartisan*education + copartisan*abortion.stance + copartisan*spending.stance + I(information.condition==3) + I(information.condition==5) + I(information.condition==7), data=estimation.data.conjoint)
attribute.comparison <- fit.w.robust(model=attribute.comparison,cluster.var='participant.id',dta=estimation.data.conjoint)

baseline.coef <- attribute.comparison$coefficients[which(names(attribute.comparison$coefficients)=="copartisan")]
copartisan.coef.rows <- grep(pattern="copartisan:", x=names(attribute.comparison$coefficients))
marginals <- baseline.coef +  attribute.comparison$coefficients[copartisan.coef.rows]

copartisan.ses <- sapply(X=copartisan.coef.rows[1:length(copartisan.coef.rows)], FUN=function(x){ sqrt(attribute.comparison$se[1,1] + attribute.comparison$se[x,x] + 2*attribute.comparison$se[1,x])})
copartisan.upper <- marginals  + 2*copartisan.ses
copartisan.lower <- marginals  - 2*copartisan.ses

plot.frame <- cbind.data.frame(marginals, copartisan.ses, copartisan.upper, copartisan.lower)
plot.frame <- plot.frame[order(marginals), ]

#Comparison between Party+Race to Party+Abortion Policy Condition Used in paper
policy.diff <- (plot.frame[9,1] - baseline.coef) - (plot.frame[1,1] - baseline.coef)
policy.diff.upper <- policy.diff + 2*plot.frame[1,2]
policy.diff.lower <- policy.diff - 2*plot.frame[1,2]
policy.diff;policy.diff.lower;policy.diff.upper

#Comparison between Party+Race to Party+Education Condition Used in paper
policy.diff.ed <- (plot.frame[9,1] - baseline.coef) - (plot.frame[3,1] - baseline.coef)
policy.diff.ed.upper <- policy.diff.ed + 2*plot.frame[1,2]
policy.diff.ed.lower <- policy.diff.ed - 2*plot.frame[1,2]
policy.diff.ed;policy.diff.ed.lower;policy.diff.ed.upper

pdf(file="attributeinteractionplot.pdf", height=4, width=5.1)
par(mar=c(4,8,2,2))
plot(x=plot.frame$marginals, y=1:length(marginals), pch=16, xlab="Effect of Co-Partisanship on Pr(Prefer Candidate)", yaxt='n', ylab='', xlim=c(.4,.85))
axis(side=2,at=1:length(marginals), labels=c('Abortion Stance', 'Spending Stance', 'Education', 'Family', 'Military Service', 'Profession', 'Age', 'Gender','Race'), las=1, cex=1.2)
segments(x0=plot.frame$copartisan.lower[1:dim(plot.frame)[1]-1], x1=plot.frame$copartisan.upper[1:(dim(plot.frame)[1]-1)], y1=1:(length(marginals)-1), y0=1:(length(marginals)-1), lwd=2)
abline(v=plot.frame$marginals[dim(plot.frame)[1]], lty=2)
points(x=plot.frame$marginals[dim(plot.frame)[1]], y=9, pch=22)
dev.off()

#############################################
#####Standard Conjoint - Info Type on Support Among Reps and Dems
#############################################

attribute.effects.republican <-  lm(formula=chosen.candidate ~ copartisan + gender.candidate + profession.candidate + age.candidate + family.status.candidate + race.candidate + education.candidate + abortion.stance.candidate + spending.stance.candidate + military.service.candidate, data=estimation.data.conjoint, subset=which(party.id=="Republican"))
attribute.effects.republican <- fit.w.robust(model=attribute.effects.republican,cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$party.id=="Republican"), ])

attribute.effects.democrat <-  lm(formula=chosen.candidate ~ copartisan + gender.candidate + profession.candidate + age.candidate + family.status.candidate + race.candidate + education.candidate + abortion.stance.candidate + spending.stance.candidate + military.service.candidate, data=estimation.data.conjoint, subset=which(party.id=="Democrat"))
attribute.effects.democrat <- fit.w.robust(model=attribute.effects.democrat,cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$party.id=="Democrat"), ])

coef.names <- names(attribute.effects.republican$coefficients)
variable.list <- c('gender.candidate','profession.candidate','age.candidate','family.status.candidate','race.candidate','education.candidate','military.service.candidate','abortion.stance.candidate','spending.stance.candidate')

#Determine Coefficient/SE rows to process
coef.rows <- sapply(X=variable.list, FUN=function(x){grep(pattern=x, x=coef.names)})
relevant.names <- lapply(X=coef.rows, FUN=function(x){coef.names[x]})
reference.categories <- unlist(lapply(X=coef.rows, FUN=function(x){coef.names[min(x)]}))

#Point Estimates for difference between parameter estimate and reference category
coef.value.list.republicans <- lapply(X=coef.rows, FUN=function(x){attribute.effects.republican$coefficients[x] - attribute.effects.republican$coefficients[min(x)]})
coef.value.list.democrats <- lapply(X=coef.rows, FUN=function(x){attribute.effects.democrat$coefficients[x] - attribute.effects.democrat$coefficients[min(x)]})

#Standard errors on the differences between parameter estimates
se.estimate.republican <- function(x){
	relevant.rows <- x
	out.ses <- NA
	for(k in 1:length(relevant.rows)){
		out.ses[k] <- sqrt(attribute.effects.republican$se[relevant.rows[k],relevant.rows[k]] + attribute.effects.republican$se[relevant.rows[1],relevant.rows[1]] - 2*attribute.effects.republican$se[relevant.rows[k],relevant.rows[1]])
	}
	return(out.ses)	
}

se.estimate.democrat <- function(x){
	relevant.rows <- x
	out.ses <- NA
	for(k in 1:length(relevant.rows)){
		out.ses[k] <- sqrt(attribute.effects.democrat$se[relevant.rows[k],relevant.rows[k]] + attribute.effects.democrat$se[relevant.rows[1],relevant.rows[1]] - 2*attribute.effects.democrat$se[relevant.rows[k],relevant.rows[1]])
	}
	return(out.ses)	
}

coef.se.list.republican <- lapply(X=coef.rows, FUN=se.estimate.republican)
coef.se.list.democrat <- lapply(X=coef.rows, FUN=se.estimate.democrat)

#########
#Attribute Plot for Republicans
#########

conjoint.attribute.frame.republican <- cbind.data.frame(unlist(coef.value.list.republicans))
names(conjoint.attribute.frame.republican) <- c('effect')
conjoint.attribute.frame.republican$se <- unlist(coef.se.list.republican)
conjoint.attribute.frame.republican$upper <- conjoint.attribute.frame.republican[,1] + 2*conjoint.attribute.frame.republican[,2]
conjoint.attribute.frame.republican$lower <- conjoint.attribute.frame.republican[,1] - 2*conjoint.attribute.frame.republican[,2]

rownames(conjoint.attribute.frame.republican) <- unlist(lapply(X=strsplit(x=rownames(conjoint.attribute.frame.republican),split="[a-z](?=[A-Z])|[a-z](?=[1-9])",perl=TRUE),FUN="[[", 2))
reference.categories <- unlist(lapply(X=strsplit(x=reference.categories,split="[a-z](?=[A-Z])|[a-z](?=[1-9])",perl=TRUE),FUN="[[", 2))

pdf(file="republicanconjoint.pdf", height=14,width=12)
par(mar=c(4,20,4,8))
plot(y=c(dim(conjoint.attribute.frame.republican)[1]:1), x=conjoint.attribute.frame.republican[,1], type='p', pch=16, ylab='', yaxt='n', xlim=c(-.35, .35), xlab="Change in Pr(Prefer Candidate)", main="Profile Attribute Effects (Republicans)")
abline(v=0,lty=2)
axis(side=2,at=dim(conjoint.attribute.frame.republican)[1]:1,labels=rownames(conjoint.attribute.frame.republican), las=1, cex.axis=.874)
segments(y0=dim(conjoint.attribute.frame.republican)[1]:1, y1=dim(conjoint.attribute.frame.republican)[1]:1, x0=conjoint.attribute.frame.republican[,3], x1=conjoint.attribute.frame.republican[,4], lwd=2)
axis(side=4, at=c(42.5, 38.5,31,23,17.5,13,9.5,7,2.5), labels=c('Gender','Occupation','Age','Family','Race','Education','Military Service','Abortion Stance','Spending Stance'), las=1)
abline(h=c(5.5,8.5, 10.5,15.5,19.5,26.5,35.5,41.5))
points(y=which(rev(rownames(conjoint.attribute.frame.republican)) %in% reference.categories),x=rep(0,length(reference.categories)), pch=22)
dev.off()

#########
#Attribute plot for Democrats
#########

conjoint.attribute.frame.democrat <- cbind.data.frame(unlist(coef.value.list.democrats))
names(conjoint.attribute.frame.democrat) <- c('effect')
conjoint.attribute.frame.democrat$se <- unlist(coef.se.list.democrat)
conjoint.attribute.frame.democrat$upper <- conjoint.attribute.frame.democrat[,1] + 2*conjoint.attribute.frame.democrat[,2]
conjoint.attribute.frame.democrat$lower <- conjoint.attribute.frame.democrat[,1] - 2*conjoint.attribute.frame.democrat[,2]

rownames(conjoint.attribute.frame.democrat) <- unlist(lapply(X=strsplit(x=rownames(conjoint.attribute.frame.democrat),split="[a-z](?=[A-Z])|[a-z](?=[1-9])",perl=TRUE),FUN="[[", 2))

pdf(file="democratconjoint.pdf", height=14,width=12)
par(mar=c(4,20,4,8))
plot(y=c(dim(conjoint.attribute.frame.democrat)[1]:1), x=conjoint.attribute.frame.democrat[,1], type='p', pch=16, ylab='', yaxt='n', xlim=c(-.35, .35), xlab="Change in Pr(Prefer Candidate)", main="Profile Attribute Effects (Democrats)")
abline(v=0,lty=2)
axis(side=2,at=dim(conjoint.attribute.frame.democrat)[1]:1,labels=rownames(conjoint.attribute.frame.democrat), las=1, cex.axis=.874)
segments(y0=dim(conjoint.attribute.frame.democrat)[1]:1, y1=dim(conjoint.attribute.frame.democrat)[1]:1, x0=conjoint.attribute.frame.democrat[,3], x1=conjoint.attribute.frame.democrat[,4], lwd=2)
axis(side=4, at=c(42.5, 38.5,31,23,17.5,13,9.5,7,2.5), labels=c('Gender','Occupation','Age','Family','Race','Education','Military Service','Abortion Stance','Spending Stance'), las=1)
abline(h=c(5.5,8.5, 10.5,15.5,19.5,26.5,35.5,41.5))
points(y=which(rev(rownames(conjoint.attribute.frame.democrat)) %in% reference.categories),x=rep(0,length(reference.categories)), pch=22)
dev.off()

#############################################
#####Robustness Checks
#############################################

#########
#Table with no issue information conditions / only consistent candidate pairing conditions
#########

no.counterstereotypical.interact.model <- lm(chosen.candidate ~ copartisan + information.condition + copartisan*information.condition, data=estimation.data.conjoint, subset=which(candidate.profile!="Inconsistent"))
no.counterstereotypical.interact.model <- fit.w.robust(model=no.counterstereotypical.interact.model,cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$candidate.profile!="Inconsistent"), ])
predicted.copartisanship.me.no.counterstereotypical <- no.counterstereotypical.interact.model$coefficients[2] + no.counterstereotypical.interact.model$coefficients[4]*seq(from=1,to=9,by=.1)

no.issue.interact.model <- lm(chosen.candidate ~ copartisan + information.condition + copartisan*information.condition, data=estimation.data.conjoint, subset=which(candidate.profile=="No.Issues"))
no.issue.interact.model  <- fit.w.robust(model=no.issue.interact.model,cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$candidate.profile=="No.Issues"), ])
predicted.copartisanship.me.no.issue <- no.issue.interact.model$coefficients[2] + no.issue.interact.model$coefficients[4]*seq(from=1,to=7,by=.1)

apsrtable(interact.model.continuous,no.counterstereotypical.interact.model, no.issue.interact.model,  model.names=c("Baseline","No Counter-Stereotypical","No Issue Content"), omitcoef=expression(grep(pattern="copartisan|information.condition|Intercept",coefnames, invert=TRUE)), caption="Pr(Prefer Candidate) By Information Amount", coef.names=c("(Intercept)", "Co-Partisan", "Information Amount", "Co-Partisan*Information Amount"), notes=list(se.note="Robust standard errors in parentheses, clustered by respondent.",stars.note="$^*$ indicates significance at $p< 0.05 $"),digits=3)

#Plot Discrete Information with Point Estimates
#No Stereotypical Discrete
no.counterstereotypical.interact.model.discrete <- lm(chosen.candidate ~ copartisan + factor(information.condition) + copartisan*factor(information.condition), data=estimation.data.conjoint, subset=which(candidate.profile!="Inconsistent"))
no.counterstereotypical.interact.model.discrete <- fit.w.robust(model=no.counterstereotypical.interact.model.discrete,cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$candidate.profile!="Inconsistent"), ])

#No Issues Discrete
no.issue.interact.model.discrete <- lm(chosen.candidate ~ copartisan + factor(information.condition) + copartisan*factor(information.condition), data=estimation.data.conjoint, subset=which(candidate.profile=="No.Issues"))
no.issue.interact.model.discrete  <- fit.w.robust(model=no.issue.interact.model.discrete, cluster.var='participant.id', dta=estimation.data.conjoint[which(estimation.data.conjoint$candidate.profile=="No.Issues"), ])

#Stereotypical Plot Frame
coparty.coefs.stereotypical <- grep(pattern='copartisan',names(no.counterstereotypical.interact.model.discrete$coefficients))
discrete.coefs.stereotypical <- c(no.counterstereotypical.interact.model.discrete$coefficients[2],sapply(X=coparty.coefs.stereotypical[2:length(coparty.coefs.stereotypical)], FUN=function(x) no.counterstereotypical.interact.model.discrete$coefficients[2] + no.counterstereotypical.interact.model.discrete$coefficients[x]))
discrete.ses.stereotypical <- c(sqrt(no.counterstereotypical.interact.model.discrete$se[2,2]),sapply(X=coparty.coefs.stereotypical[2:length(coparty.coefs.stereotypical)], FUN=function(x) sqrt(no.counterstereotypical.interact.model.discrete$se[2,2] + no.counterstereotypical.interact.model.discrete$se[x,x] + 2*no.counterstereotypical.interact.model.discrete$se[2,x])))
discrete.interact.plot.frame.stereotypical <- cbind.data.frame(discrete.coefs.stereotypical, discrete.ses.stereotypical)
discrete.interact.plot.frame.stereotypical$upper <- discrete.interact.plot.frame.stereotypical[,1] + 2*discrete.interact.plot.frame.stereotypical[,2]
discrete.interact.plot.frame.stereotypical$lower <- discrete.interact.plot.frame.stereotypical[,1] - 2*discrete.interact.plot.frame.stereotypical[,2]

#No Issue Plot Frame
coparty.coefs.noissue <- grep(pattern='copartisan',names(no.issue.interact.model.discrete$coefficients))
discrete.coefs.noissue <- c(no.issue.interact.model.discrete$coefficients[2],sapply(X=coparty.coefs.noissue[2:length(coparty.coefs.noissue)], FUN=function(x) no.issue.interact.model.discrete$coefficients[2] + no.issue.interact.model.discrete$coefficients[x]))
discrete.ses.noissue <- c(sqrt(no.issue.interact.model.discrete$se[2,2]),sapply(X=coparty.coefs.noissue[2:length(discrete.coefs.noissue)], FUN=function(x) sqrt(no.issue.interact.model.discrete$se[2,2] + no.issue.interact.model.discrete$se[x,x] + 2*no.issue.interact.model.discrete$se[2,x])))
discrete.interact.plot.frame.noissue <- cbind.data.frame(discrete.coefs.noissue, discrete.ses.noissue)
discrete.interact.plot.frame.noissue$upper <- discrete.interact.plot.frame.noissue[,1] + 2*discrete.interact.plot.frame.noissue[,2]
discrete.interact.plot.frame.noissue$lower <- discrete.interact.plot.frame.noissue[,1] - 2*discrete.interact.plot.frame.noissue[,2]

#Consistent Profiles / No Issue Content
pdf(file='copartisanshipeffectdiscrete-subsets.pdf', height=3, width=7.25)
par(mfrow=c(1,2), mar=c(4,5.5,1.5,4))
plot(y=discrete.interact.plot.frame.stereotypical[,1],x=c(1,3,5,7,9), xlab='Pieces of Candidate Information', las=1, xaxt='n', ylim=c(0,1), ylab='Effect of Co-Partisanship on
Pr(Prefer Candidate)',main="No Counter-Stereotypical", pch=16, cex=1.5, type='n')
lines(x=seq(from=1,to=9,by=.1), y=predicted.copartisanship.me.no.counterstereotypical,col='gray', lwd=3)
points(discrete.interact.plot.frame.stereotypical[,1],x=c(1,3,5,7,9), pch=16, cex=1.25)
axis(side=1, at=c(1,3,5,7,9))
segments(x0=c(1,3,5,7,9), x1=c(1,3,5,7,9), y1=discrete.interact.plot.frame.stereotypical[,3], y0=discrete.interact.plot.frame.stereotypical[,4], lwd=2.5)

plot(y=discrete.interact.plot.frame.noissue[,1],x=c(1,3,5,7), xlab='Pieces of Candidate Information', las=1, xaxt='n', ylim=c(0,1), ylab='Effect of Co-Partisanship on
Pr(Prefer Candidate)',main="No Issue Info", pch=16, cex=1.5, type='n')
lines(x=seq(from=1,to=7,by=.1), y=predicted.copartisanship.me.no.issue,col='gray', lwd=3)
points(discrete.interact.plot.frame.noissue[,1],x=c(1,3,5,7), pch=16, cex=1.25)
axis(side=1, at=c(1,3,5,7))
segments(x0=c(1,3,5,7), x1=c(1,3,5,7), y1=discrete.interact.plot.frame.noissue[,3], y0=discrete.interact.plot.frame.noissue[,4], lwd=2.5)
dev.off()

#Difference on low to high information conditions, no counterstereotypical info
summary(no.counterstereotypical.interact.model.discrete)
nocounter.diff <- no.counterstereotypical.interact.model.discrete$coefficients[10]
nocounter.upper <- nocounter.diff + 2*sqrt(no.counterstereotypical.interact.model.discrete$se[10,10])
nocounter.lower <- nocounter.diff - 2*sqrt(no.counterstereotypical.interact.model.discrete$se[10,10])
nocounter.diff;nocounter.lower;nocounter.upper

#Difference on low to high information conditions, no issue info
summary(no.issue.interact.model.discrete)
noissue.diff <- no.issue.interact.model.discrete$coefficients[7]
noissue.upper <- noissue.diff + 2*sqrt(no.issue.interact.model.discrete$se[7,7])
noissue.lower <- noissue.diff - 2*sqrt(no.issue.interact.model.discrete$se[7,7])
noissue.diff; noissue.lower; noissue.upper

#########
#Subset to First Task to remove possibility of carryover effects within subject
#########
interact.model.continuous.firsttask <- lm(chosen.candidate ~ copartisan + information.condition + copartisan*information.condition, data=estimation.data.conjoint[which(estimation.data.conjoint$choice.task.number==1), ])
interact.model.continuous.firsttask <- fit.w.robust(model=interact.model.continuous.firsttask,cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$choice.task.number==1), ])

interact.model.continuous.w.infotype.firsttask <-  lm(chosen.candidate ~ copartisan + information.condition + copartisan*information.condition + gender + profession + age + family.status + race + education + abortion.stance + spending.stance, data=estimation.data.conjoint[which(estimation.data.conjoint$choice.task.number==1),])
interact.model.continuous.w.infotype.firsttask <- fit.w.robust(model=interact.model.continuous.w.infotype.firsttask, cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$choice.task.number==1),])

interact.model.continuous.typeandcontent.firsttask <-  lm(formula=chosen.candidate ~ copartisan + information.condition + copartisan*information.condition + gender.candidate + profession.candidate + age.candidate + family.status.candidate + race.candidate + education.candidate + abortion.stance.candidate + spending.stance.candidate + I(military.service.candidate=="Served in U.S. Military"), data=estimation.data.conjoint[which(estimation.data.conjoint$choice.task.number==1),] )
interact.model.continuous.typeandcontent.firsttask <- fit.w.robust(model=interact.model.continuous.typeandcontent.firsttask,cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$choice.task.number==1),])

apsrtable(interact.model.continuous.firsttask, interact.model.continuous.w.infotype.firsttask, interact.model.continuous.typeandcontent.firsttask,  model.names=c("Baseline","Controls: Info Type","Controls: Info Type + Content"), omitcoef=expression(grep(pattern="copartisan|information.condition|Intercept",coefnames, invert=TRUE)), caption="Pr(Prefer Candidate) By Information Amount", coef.names=c("(Intercept)", "Co-Partisan", "Information Amount", "Co-Partisan*Information Amount"), notes=list(se.note="Robust standard errors in parentheses, clustered by respondent.",stars.note="$^*$ indicates significance at $p< 0.05 $"),digits=3)

#########
#Attribute Effects with party - Only for those in the first information condition
#########
attribute.comparison.only1 <- lm(chosen.candidate ~ copartisan + gender + profession + age + family.status + military.service + education + abortion.stance + spending.stance + copartisan*gender + copartisan*profession + copartisan*age + copartisan*family.status + copartisan*military.service + copartisan*education + copartisan*abortion.stance + copartisan*spending.stance, data=estimation.data.conjoint[which(estimation.data.conjoint$information.condition==1), ])
attribute.comparison.only1 <- fit.w.robust(model=attribute.comparison.only1, cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$information.condition==1), ])

apsrtable(attribute.comparison.only1, model.names=c('Only Info Condition 1'), caption="Co-Partisanship Effect by Available Information Type", notes=list(se.note="Robust standard errors in parentheses, clustered by respondent.", stars.note="$^*$ indicates significance at $p< 0.05 $"),digits=3)

#########
#Profile Length and Time on Page
#########
information.condition.latency <-  lm(choice.time ~ factor(information.condition), data=estimation.data.conjoint)
information.condition.latency <- fit.w.robust(model=information.condition.latency, cluster.var='participant.id',dta=estimation.data.conjoint)

information.condition.latency.log <-  lm(log(choice.time) ~ factor(information.condition), data=estimation.data.conjoint)
information.condition.latency.log <- fit.w.robust(model=information.condition.latency.log, cluster.var='participant.id',dta=estimation.data.conjoint)

apsrtable(information.condition.latency,information.condition.latency.log,model.names=c('Seconds','Log(Seconds)'),caption="Response Latency by Information Condition",coef.names=c('(Intercept)','3 Additional Pieces of Information','5 Additional Pieces of Information','7 Additional Pieces of Information','9 Additional Pieces of Information'))

#########
#Effect of Prior Conjoint Lenth on Use of Partisanship
#########
prior.length <- estimation.data.conjoint[,c('participant.id','choice.task.number','information.condition','candidate.order')]
names(prior.length) <- c('participant.id','choice.task.number','information.condition.prior','candidate.order')
prior.length <- subset(prior.length,prior.length$choice.task.number!=3)
prior.length$choice.task.number <- prior.length$choice.task.number + 1

estimation.data.conjoint.prior <- merge(estimation.data.conjoint,prior.length,by=c('participant.id','choice.task.number','candidate.order'),all.x=TRUE)

control.prior.length.model <- lm(chosen.candidate ~ copartisan + information.condition + copartisan*information.condition + factor(information.condition.prior),data=estimation.data.conjoint.prior)
control.prior.length.model <- fit.w.robust(model=control.prior.length.model,cluster.var='participant.id',dta=estimation.data.conjoint.prior)
apsrtable(control.prior.length.model)

prior.length.model.9 <- lm(chosen.candidate ~ copartisan + information.condition + copartisan*information.condition,data=estimation.data.conjoint[which(estimation.data.conjoint.prior$information.condition.prior==9),])
prior.length.model.9 <- fit.w.robust(model=prior.length.model.9,cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint.prior$information.condition.prior==9),])
apsrtable(prior.length.model.9)

prior.length.model.less <- lm(chosen.candidate ~ copartisan + information.condition + copartisan*information.condition,data=estimation.data.conjoint.prior[which(estimation.data.conjoint.prior$information.condition.prior < 9),])
prior.length.model.less <- fit.w.robust(model=prior.length.model.less,cluster.var='participant.id',dta=estimation.data.conjoint.prior[which(estimation.data.conjoint.prior$information.condition.prior < 9),])

apsrtable(prior.length.model.9,prior.length.model.less,model.names=c('9 Traits in Prior Profile','Less than 9 Traits in Prior Profile'),caption="Comparing Use of Partisanship Based on Prior Profile Length")

#########
#Assessing Potential Recency Bias for Long Profiles
#########
less.5 <- lm(chosen.candidate ~ copartisan*information.condition, data=estimation.data.conjoint[which(estimation.data.conjoint$information.condition < 5),])
less.5 <- fit.w.robust(model=less.5,cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$information.condition < 5),])

less.7 <- lm(chosen.candidate ~ copartisan*information.condition, data=estimation.data.conjoint[which(estimation.data.conjoint$information.condition < 7),])
less.7 <- fit.w.robust(model=less.7,cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$information.condition < 7),])

less.9 <- lm(chosen.candidate ~ copartisan*information.condition, data=estimation.data.conjoint[which(estimation.data.conjoint$information.condition < 9),])
less.9 <- fit.w.robust(model=less.9,cluster.var='participant.id',dta=estimation.data.conjoint[which(estimation.data.conjoint$information.condition < 9),])

full <- lm(chosen.candidate ~ copartisan*information.condition, data=estimation.data.conjoint)
full <- fit.w.robust(model=full,cluster.var='participant.id',dta=estimation.data.conjoint)

apsrtable(less.5,less.7,less.9,full,model.names=c('1 thru 3 Attributes','1 thru 5 Attributes','1 thru 7 Attributes','All Conditions'),caption="Decline in Partisan Voting by Information Amount: Subset by Profile Lengths")

#########
#IV Reg: Information Condition -> Latency -> Cue Use
#########
library(AER)
library(texreg)

iv.latency <- ivreg(chosen.candidate ~ copartisan + choice.time + copartisan*choice.time + gender.candidate + profession.candidate + age.candidate + family.status.candidate + race.candidate + education.candidate + abortion.stance.candidate + spending.stance.candidate + I(military.service.candidate=="Served in U.S. Military") | copartisan + factor(information.condition) + copartisan*factor(information.condition) + gender.candidate + profession.candidate + age.candidate + family.status.candidate + race.candidate + education.candidate + abortion.stance.candidate + spending.stance.candidate + I(military.service.candidate=="Served in U.S. Military"),data=estimation.data.conjoint)
iv.latency <- fit.w.robust(model=iv.latency,cluster.var="participant.id",dta=estimation.data.conjoint)
texreg(iv.latency,override.pval=list(c( coeftest(iv.latency, vcov=iv.latency$se)[,4])),override.se=list(c(sqrt(diag(iv.latency$se)))),stars=c(0.05),caption="Effect of Instrumented Response Latency on Use of Partisanship",digits=3)